package com.powerunion.extjs.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.newsoft.dwr.BasePara;
import com.newsoft.dwr.ListRange;
import com.newsoft.dwr.XmlParser;

public class FormDataLoader extends DbDataLoader {
	
	public FormDataLoader(BasePara paravo){
		super(paravo);
	}
	
	public void wrapSql() throws Exception{
		XmlParser parser = new XmlParser();
		this.paravo.setXmlFileName("form_sql_config.xml");
		parser.loadSql(this.paravo);
		this.paravo.addWhereSql();
		this.paravo.addOrderSql();
		this.paravo.setPaging(false);//no paging
	}
	
	public void collectToMap() {
		ArrayList<Object> al = null;
		HashMap<String,Object> map = null;
		String[] colNameArray = convert.contvertColumnName( this.paravo.getSql());
		if (!rs.isEmpty()) {
			al = new ArrayList<Object>();
			for (Iterator<?> it = rs.iterator(); it.hasNext();) {
				Object[] obj = null;
				if(colNameArray.length==1){
					obj = new Object[]{it.next()};
				}else{
					obj = (Object[]) it.next();
				}
				map = new HashMap<String,Object>();
				for (int i = 0; i < colNameArray.length; i++) {
					String tag = colNameArray[i];
					map.put(tag, obj[i]);
				}
				al.add(map);
				break;
			}
		}
		range = new ListRange();
		range.setData(al);
		range.setTotalSize(this.totalCount);
//		this.setMetaData(colNameArray);
	}
}
